package wator.model;

/**
 * 
 */
public class SimulationThread extends Thread {

	private static SimulationThread t = null;
	
	private static volatile boolean run = false;
	private static volatile long sleepTime = 1000;
	
	private SimulationThread() {
		super();
	}
	
	synchronized public static void startSimulation() {
		if (run == false) {
			run = true;
			if (t == null || !t.isAlive()) {
				t = new SimulationThread();
				t.start();
			}
		}
	}
	
	synchronized public static void stopSimulation() {
		run = false;
	}
	
	public static void setSleepTime(long time) {
		sleepTime = time;
	}
	
	public static long getSleepTime() {
		return sleepTime;
	}

	/* (non-Javadoc)
	 * @see java.lang.Thread#run()
	 */
	@Override
	public void run() {
		try {
			while (run) {
				Simulation.getInstance().iterate(1);
				Thread.sleep(sleepTime);
			}
		} catch (InterruptedException e) {
			; // Just finish
		}
	}
}
